package com.jiangong.service.impl;


import com.jiangong.service.DeepSeekService;
import okhttp3.*;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class DeepSeekServiceImpl implements DeepSeekService {
    // 替换为你的 API Key
    private static final String API_KEY = "Bearer sk-56169048534f4fb5aab3bd54a1c56a54";
    private static final String API_URL = "https://api.deepseek.com/chat/completions";

    public  String getJson(String context) throws IOException {
        // 1. 创建请求体（JSON）
        String jsonBody = String.format("{\n"
                + "  \"model\": \"deepseek-chat\",\n"
                + "  \"messages\": [\n"
                + "    {\"role\": \"user\", \"content\": \"%s\"}\n"
                + "  ],\n"
                + "  \"temperature\": 0.7,\n"
                + "  \"top_p\": 0.8\n"
                + "}", context);
        // 2. 创建 HTTP 请求
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.get("application/json"), jsonBody);

        Request request = new Request.Builder()
                .url(API_URL)
                .post(body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .addHeader("Authorization", API_KEY) // 直接使用API Key
                .build();

        // 3. 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("请求失败: " + response);
            }
           return  response.body().string();
        }

    }

}
